<?php
/**
 * @file   admin.php
 * @date   2017-11-15 15:22:57
 * @author huwhis<huuwhois>
 * @version   0.0.1
 */

namespace app\admin\controller;

use app\admin\model\AdminUser as UserModel;
use think\Request;

class Adminuser extends Base
{
    protected $adminuser_model;
    protected $adminuser_info;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new UserModel();
        $this->adminuser_info = $this->model->field(true)->find($this->uid);
    }

    public function index()
    {
        $data = $this->model->field('id,uname,role_id,pen_name,email,ctime,status')->select();

        $data_role = model('admin_role')->column('id, name');

        $this->assign("data", $data);
        $this->assign("data_role", $data_role);
        return $this->fetch();
    }
    
    public function add()
    {
        if (Request::instance()->isPOST()) {
            $param = input('post.');
            // var_dump($param);
            // exit;

            if ($param['username'] =='' || $param['password'] =='' || $param['email'] =='') {
                $this->error("用户名 or 密码 or 邮箱 不能为空!");
            }
    
            try {
                $this->model->save([
                    'uname' => $param['username'],
                    'role_id' => intval($param['role_id']),
                    'password' => md5($param['password']),
                    'pen_name' => $param['pen_name'],
                    'email' => $param['email'],
                    'description' => $param['description'],
                    'ctime' => time()
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:".$msg);
            }
                $this->success('添加成功', 'index');
        } else {
            $data_role = model('admin_role')->where('id', '<>', 1)->column('id, name');
           
            $this->assign("data_role", $data_role);

            return $this->fetch();
        }
    }

    public function edit($id)
    {
        $data = $this->model->find($id);

        if (Request::instance()->isPOST()) {
            $param = input('post.');
            // var_dump(input('post.'));
            // exit;
            
            if ($param['username'] ==''  || $param['email'] =='') {
                $this->error("用户名  or 邮箱 不能为空!");
            }

            try {
                $this->model->save([
                    'uname' => $param['username'],
                    'role_id' => intval($param['role_id']),
                    'pen_name' => $param['pen_name'],
                    'email' => $param['email'],
                    'description' => $param['description'],
                    'ctime' => time()
                ], ['id'=>$id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误代码:".$msg);
            }
                $this->success('编辑成功', 'index');
        } else {
            $data_role = model('admin_role')->where('id', '<>', 1)->column('id, name');

            $this->assign("data_role", $data_role);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if (is_array($id)) {
                if (in_array('1', $id)) {
                    return ['status'=>0,'msg'=>'此用户无法删除'];
                }
                if (in_array(session('uid'), $id)) {
                    return ['status'=>0,'msg'=>'当前登录用户无法删除'];
                }
            } else {
                if ($id == 1) {
                    return ['status'=>0,'msg'=>'此用户无法删除'];
                }
                if ($id == session('uid')) {
                    return ['status'=>0,'msg'=>'当前登录用户无法删除'];
                }
            }
            if ($this->model->destroy($id)) {
                return ['code' => 2,'msg'=>'删除成功'];
            } else {
                return ['code' => 0,'msg'=>'删除失败'];
            }
        }
    }

//停用管理员
    public function stop($id = '', $isban = 0)
    {
        if (Request::instance()->isAjax()) {
            if ($id == 1) {
                return ['status'=>0,'msg'=>'超级管理员禁止停用'];
            }
            if ($this->adminuser_model->save(['isban' =>1], ['id' => $id])) {
                return ['status'=>1,'msg'=>'停用成功'];
            } else {
                return ['status'=>0,'msg'=>'停用失败'];
            }
        }
    }
//启用用管理员
    public function start($id = '', $isban = 0)
    {
        if (Request::instance()->isAjax()) {
            if ($id == 1) {
                return ['status'=>0,'msg'=>'超级管理员禁止停用'];
            }
            if ($this->adminuser_model->save(['isban' =>0], ['id' => $id])) {
                return ['status'=>1,'msg'=>'启用成功'];
            } else {
                return ['status'=>0,'msg'=>'启用失败'];
            }
        }
    }
    
    
//修改密码
    public function modifyPwd()
    {
        $pwd = $this->adminuser_info->password;
    //ajax验证密码
        if (Request::instance()->isAjax()) {
            $password = md5(trim(input('post.password')));
            if ($pwd==$password) {
                return ['status'=>1];
            } else {
                return ['status'=>0];
            }
        } elseif (Request::instance()->isPOST()) {
            $oldpwd = md5(trim(input('post.oldpassword')));
            $newpwd = md5(trim(input('post.newpassword')));
            $repwd = md5(trim(input('post.repassword')));
            if ($pwd!=$oldpwd) {
                $this->error('原密码不正确');
            }
            if ($newpwd!=$repwd) {
                $this->error('两次新密码不一致，请核查');
            }
            $this->adminuser_info->password = $newpwd;
            if ($this->adminuser_info->save()) {
                session(null);
                $this->success("修改成功,请重新登陆", 'login/logout');
            } else {
                $this->error('修改失败，请稍后重试');
            }
        } else {
            return $this->fetch();
        }
    }
}
